﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Sac.Web;

public partial class LeftMenuTree : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CreateMenuTree();
        }
    }

    protected void CreateMenuTree()
    {
        string menuId = "";
        try
        {
            menuId = Request.QueryString[0].ToString();
        }
        catch
        {
            Server.Transfer("ErrorPage.aspx");
        }
        MenuTreeOperate root = new MenuTreeOperate();
        string rootImgUrl = root.GetRootImgUrl(menuId);
        if (rootImgUrl != "" && rootImgUrl != null)
            imgRoot.ImageUrl = rootImgUrl;
        DataTable dtChild = root.GetAllMenuNode();
        InitTree(treeMenu.Nodes, menuId, dtChild);
    }

    protected void InitTree(TreeNodeCollection tnc, string fartherId, DataTable dtMenuList)
    {
        DataView dvChild = new DataView();
        dvChild.Table = dtMenuList;
        dvChild.RowFilter = "fartherid=" + fartherId;
        foreach (DataRowView drvMenuItem in dvChild)
        {
            try
            {
                int i = Session["RolePower"].ToString().IndexOf(drvMenuItem["ID_KEY"].ToString() + "|");
                if (i >= 0)
                {
                    TreeNode nodeTemp = new TreeNode();
                    nodeTemp.Value = drvMenuItem["ID_KEY"].ToString();
                    nodeTemp.Text = drvMenuItem["MENU_NAME"].ToString();
                    nodeTemp.ImageUrl = drvMenuItem["MENU_IMG_URL"].ToString();
                    nodeTemp.NavigateUrl = drvMenuItem["MENU_URL"].ToString();
                    //nodeTemp.NavigateUrl = "RightPage.aspx?id=" + drvMenuItem["ID_KEY"].ToString();
                    nodeTemp.SelectAction = TreeNodeSelectAction.SelectExpand;
                    nodeTemp.Target = "showMain";
                    tnc.Add(nodeTemp);
                    InitTree(nodeTemp.ChildNodes, nodeTemp.Value, dtMenuList);
                }
            }
            catch { }
        }
    }
}
